﻿namespace OverStore.Sql.TableSchema
{
    /// <summary>
    /// Base untyped key for <see cref="Table"/>
    /// </summary>
    public abstract class KeyBase : ColumnBase
    {
        private string _OriginalIdentifierName;
        private string _NewIdentifierName;

        /// <summary>
        /// Gets or sets the name of the original immutable identifier result.
        /// </summary>
        public string OriginalIdentifierName
        {
            get { return _OriginalIdentifierName ?? "orig__" + Name; }
            set { _OriginalIdentifierName = value; }
        }

        /// <summary>
        /// Gets or sets the name of new identifier generated by INSERT statement.
        /// </summary>
        public string NewIdentifierName
        {
            get { return _NewIdentifierName ?? "new__" + Name; }
            set { _NewIdentifierName = value; }
        }

        /// <summary>
        /// Gets or sets a result indicating whether this primary key column is mutable.
        /// </summary>
        /// <remarks>
        /// If key column is immutable, that means it's result can't be changed by INSERT or UPDATE statement, except
        /// case when it's result is changed by database, for example, by using output parameter etc. 
        /// During building INSERT command immutable columns always uses <see cref="KeyBase.OriginalIdentifierName"/>
        /// parameters for key column values. Due building SET part of UPDATE command, immutable key columns are not included there.
        /// </remarks>
        public bool IsMutable { get; set; }

        /// <summary>
        /// When overriden in derived classes, returns actual column role in table.
        /// </summary>
        public override ColumnType ColumnType
        {
            get
            {
                return ColumnType.PrimaryKey;
            }
        }

        /// <summary>
        /// Gets or sets a result indicating whether the result of this primary key is generated by database.
        /// </summary>
        public bool IsDatabaseGenerated { get; set; }

        /// <summary>
        /// Get or set result defines is column should be skipped on generating INSERT statement.
        /// </summary>
        /// <value></value>
        public override bool SkipOnInsert
        {
            get
            {
                if (IsDatabaseGenerated)
                    return true;
                return base.SkipOnInsert;
            }
            set
            {
                base.SkipOnInsert = value;
            }
        }

        /// <summary>
        /// Gets or sets the optional name of identifier generator. 
        /// Use this value only for RDBMS provides last inserted identifier by defining name of identifier generator 
        /// (for example, Oracle uses sequences for generates new identifier, so here must be name of sequence).
        /// </summary>
        public string IdentifierGeneratorName { get; set; }
    }
}
